package chelydra.sharelydra.client.webservice;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Hashtable;
import java.util.Map;

/**
 *
 * @author Pierre-Élie Fauché
 */
public class WebserviceManager extends URLFactory {

	/**
	 * XMLParser used to parse the responses from the server.
	 */
	private XMLParser parser;

	/**
	 * Instanciate a new WebserviceManager and a XMLParser.
	 */
	public WebserviceManager() {
		super();
		this.parser = new XMLParser(this);
	}

	public WebserviceManager(String userLogin, String userPassword) {
		this();
		this.setUserLogin(userLogin);
		this.setUserPassword(userPassword);
	}

	public String doRegister(Map<String,String> infos) throws WebserviceException {
		return this.doAction(this.getURLForRegister(infos));
	}

	public String doConnect(Integer port) throws WebserviceException {
		String url = this.getURLForAction("connect");
		url = this.addParameterToURL(url, "port", port.toString());
		return this.doAction(url);
	}

	public void doDisconnect() throws WebserviceException {
		this.doAction(this.getURLForAction("disconnect"));
	}

	public void doFriends() throws WebserviceException {
		this.doAction(this.getURLForAction("infos"));
	}

	public void doPropose(String login) throws WebserviceException {
		String url = this.getURLForAction("propose");
		url = this.addParameterToURL(url, "proposed", login);
		this.doAction(url);
	}

	public void doSearchFriends(String keywordsString) throws WebserviceException {
		String url = this.getURLForAction("search");
		url = this.addParameterToURL(url, "keywords", keywordsString);
		this.doAction(url);
	}

	public void doProfile(Map<String,String> parameters)  throws WebserviceException {
		String url = this.getURLForAction("profile");
		url = this.addParametersToURL(url, parameters);
		this.doAction(url);
	}

	public void doAccept(String login) throws WebserviceException {
		String url = this.getURLForAction("accept");
		url = this.addParameterToURL(url, "proposing", login);
		this.doAction(url);
	}

	public void doDecline(String login) throws WebserviceException {
		String url = this.getURLForAction("decline");
		url = this.addParameterToURL(url, "proposing", login);
		this.doAction(url);
	}

	public void doRemove(String login) throws WebserviceException {
		String url = this.getURLForAction("remove");
		url = this.addParameterToURL(url, "removed", login);
		this.doAction(url);
	}

	public void doNotify(String file, String friends) throws WebserviceException {
		Map<String,String> parameters = new Hashtable();
		parameters.put("file", file);
		parameters.put("friends", friends);

		String url = this.getURLForAction("notify");
		url = this.addParametersToURL(url, parameters);
		this.doAction(url);
	}

	private String doAction(String urlString) throws WebserviceException {
		String result = "An error occurred";
		try {
			URL url = new URL(urlString);
			System.err.println("*** XMLParser *** URL:: "+url.toString());
			HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
			httpConnection.connect();
			result = this.parser.processResponse(httpConnection.getInputStream());
			httpConnection.disconnect();
		} catch (Exception ex) {
			throw new WebserviceException(ex.getMessage());
		}
		return result;
	}

}
